package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.domain.*;
import com.service.IDoctorService;
import com.service.IDrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private IDoctorService iDoctorService;

    @GetMapping
    public Result getAll(){
        List<Doctor> list = iDoctorService.list();
        if(list!=null){
            return new Result(list,"查询成功", Code.GET_OK);
        }else
        {
            return new Result("查询失败", Code.GET_ERR);
        }

    }

    @GetMapping("/{doctorName}")
    public  Result getByName(@PathVariable String doctorName){
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("doctor_name",doctorName);
        List<Doctor> list = iDoctorService.list(queryWrapper);
        if(list.isEmpty()){
            return new Result("查询失败", Code.GET_ERR);
        }else
        {
            return new Result(list,"查询成功", Code.GET_OK);
        }
    }

    @PostMapping
    public Result add(@RequestBody Doctor doctor){
        boolean flag = iDoctorService.save(doctor);
        if(flag){
            return new Result("添加成功", Code.SAVE_OK);
        }else
        {
            return new Result("添加失败", Code.SAVE_ERR);
        }
    }

    @DeleteMapping("/{doctorId}")
    public Result deleteById(@PathVariable Integer doctorId){
        boolean flag = iDoctorService.removeById(doctorId);
        if(flag){
            return new Result("删除成功", Code.DELETE_OK);
        }else
        {
            return new Result("删除失败", Code.DELETE_ERR);
        }
    }

    @PutMapping
    public Result update(@RequestBody Doctor doctor){
        boolean flag = iDoctorService.updateById(doctor);
        if(flag){
            return new Result("修改成功", Code.UPDATE_OK);
        }else
        {
            return new Result("修改失败", Code.UPDATE_ERR);
        }
    }

    
}

